﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithm
{
    public class TreeNode
    {
        public int data;
        public TreeNode left;
        public TreeNode right;
        public TreeNode parent;

        public TreeNode(int d)
        {
            data = d;
        }

        public int height()
        {
            int leftHeight = left != null ? left.height() : 0;
            int rightHeight = right != null ? right.height() : 0;

            return 1 + Math.Max(leftHeight, rightHeight);
        }

        public TreeNode find(int d)
        {
            if (d == data)
                return this;
            else if (d <= data)
                return left != null ? left.find(d) : null;
            else if (d > data)
                return right != null ? right.find(d) : null;

            return null;
        }

        public void setLeftChild(TreeNode left)
        {
            this.left = left;
            if (left != null)
                left.parent = this;
        }

        public void setRightChild(TreeNode right)
        {
            this.right = right;
            if (right != null)
                right.parent = this;
        }
    }
}
